<?php

namespace Home\Controller;
use Think\Controller;
class WxpayController extends Controller
{
public function _initialize()
{
	//引入WxPayPubHelper
	vendor('WxPayPubHelper.WxPayPubHelper');
}

public function native_pay()
{
		//设置静态链接
		$nativeLink = new \NativeLink_pub();

		//设置静态链接参数
		//设置必填参数
		//appid已填,商户无需重复填写
		//mch_id已填,商户无需重复填写
		//noncestr已填,商户无需重复填写
		//time_stamp已填,商户无需重复填写
		//sign已填,商户无需重复填写
		$product_id = time();//自定义商品id
		$nativeLink->setParameter("product_id",$product_id);//商品id
		//获取链接
		$product_url = $nativeLink->getUrl();
		//使用短链接转换接口
		$shortUrl = new \ShortUrl_pub();
		//设置必填参数
		//appid已填,商户无需重复填写
		//mch_id已填,商户无需重复填写
		//noncestr已填,商户无需重复填写
		//sign已填,商户无需重复填写
		$shortUrl->setParameter("long_url",$product_url);//URL链接
		//获取短链接
		$codeUrl = $shortUrl->getShortUrl();
		$this->assign('product_url',$product_url);
		$this->assign('codeUrl',$codeUrl);
		echo $codeUrl;exit();
		$this->display();

}

public function createQrcode($order_sn=1234567890)
		{
				//使用统一支付接口
				$unifiedOrder = new \UnifiedOrder_pub();

				//设置统一支付接口参数
				//设置必填参数
				//appid已填,商户无需重复填写
				//mch_id已填,商户无需重复填写
				//noncestr已填,商户无需重复填写
				//spbill_create_ip已填,商户无需重复填写
				//sign已填,商户无需重复填写
				$unifiedOrder->setParameter("body","听说科技-$order_sn");//商品描述
				//自定义订单号，此处仅作举例
				$db_order = M("order");
				$orderdata = $db_order->field('money')->where(array("number"=>$order_sn))->find();
				$money = $orderdata['money']*100;
				$money = 1;
				// $order_sn = $order_sn.time();
				$notify_url = "http://".ADMIN_URL."Home/Wxpay/wx_hui";
				$unifiedOrder->setParameter("out_trade_no","$order_sn");//商户订单号
				$unifiedOrder->setParameter("total_fee","$money");//总金额
				$unifiedOrder->setParameter("notify_url", "$notify_url");//通知地址
				$unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
				//非必填参数，商户可根据实际情况选填
				//$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
				//$unifiedOrder->setParameter("device_info","XXXX");//设备号
				//$unifiedOrder->setParameter("attach","XXXX");//附加数据
				//$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
				//$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间
				//$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记
				//$unifiedOrder->setParameter("openid","XXXX");//用户标识
				//$unifiedOrder->setParameter("product_id","XXXX");//商品ID

				//获取统一支付接口结果
				$unifiedOrderResult = $unifiedOrder->getResult();

				//商户根据实际情况设置相应的处理流程
				if ($unifiedOrderResult["return_code"] == "FAIL")
				{
						//商户自行增加处理流程
						echo "通信出错：".$unifiedOrderResult['return_msg']."<br>";
				}
				elseif($unifiedOrderResult["result_code"] == "FAIL")
				{
						//商户自行增加处理流程
						echo "错误代码：".$unifiedOrderResult['err_code']."<br>";
						echo "错误代码描述：".$unifiedOrderResult['err_code_des']."<br>";
				}
				elseif($unifiedOrderResult["code_url"] != NULL)
				{
						//从统一支付接口获取到code_url
						$code_url = $unifiedOrderResult["code_url"];
						//商户自行增加处理流程
						//......
				}
				// echo $code_url;exit();
				$fileName = createQRcode($code_url,1,$order_sn);
				$data['fileName'] = $fileName;
				$data['out_trade_no'] = $order_sn;
				$data['code_url'] = $code_url;
				$data['unifiedOrderResult'] = $unifiedOrderResult;
				// p($data);exit();
				return $data;
				// echo $fileName;exit();
				// $this->assign('path',$fileName);
				// $this->assign('out_trade_no',$out_trade_no);
				// $this->assign('code_url',$code_url);
				// $this->assign('unifiedOrderResult',$unifiedOrderResult);

				// $this->display();
		}


//微信回调
	public function wx_hui()
	{
		$msg = array();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$order_sn = $msg['out_trade_no'];
		// M('orderqrcode')->add(array("order_sn"=>$order_sn));exit();
		if ($order_sn) {
			$db_order = M("order");
			$orderdata = $db_order->field('id,mid,is_collect,number,level,status')->where(array("number"=>$order_sn))->find();
			if ($orderdata['status'] == 1) {
				$qrdata['type'] = 1;
				$qrdata['paytime'] = date("Y-m-d H:i:s",time());
				M('orderqrcode')->where(array('order_sn'=>$order_sn))->save($qrdata);//更新数据
				$paydata['status'] = 2;
				$paydata['starttime'] = time();
				$this->order_push($orderdata['id']);
			}
		}
		// $this->success('支付成功，已通知主播接单',"/Home/Order/order_list",1);
	}

public function wx_order_push($oid){
	$orderdata = M("order")->field('id,mid,is_collect,number,level,texttype')->where(array("id"=>$oid))->find();
	$is_collect = $orderdata['is_collect'];
	$result = $orderdata['id'];
	$number = $orderdata['number'];
	$mid = $orderdata['mid'];
	$level = $orderdata['level'];
	$label = $orderdata['texttype'];
	if ($is_collect == 0) {
		/*给对应等级符合条件的主播群发*/
		$where = " 1=1 ";
		$labeldata = M("label")->select();//读取配置
		foreach ($labeldata as $k => $v) {
				if($v['label_name'] == $label){$labelkey = $v['label_id'];}
		}
		$db_member = M("member");
		$db_order = M("order");
		$ordersex = $db_order->field('sex')->where(array('id'=>$result))->find()['sex'];
		if ($ordersex != 0) {
			$where .= " and sex = $ordersex";
		}
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value desc')->select();
		foreach ($leveldata as $key => $value) {
			if ($level == $value['id']) {
				if ($key == 0) {
					$where .= " and status = 9 and level >=$value[value] and articles like '%$labelkey%'";
					$level_user = $db_member->field('uuid')->where($where)->select();
				}else{
					$last_value = $leveldata[$key-1]['value'];
					$where .= " and status = 9 and level >=$value[value] and level < $last_value and articles like '%$labelkey%'";
					$level_user = $db_member->field('uuid')->where($where)->select();
				}
			}
		}

		// 根据时间段再次筛选
		/*foreach ($level_user as $key => $value) {
				$user_timetype = $db_member->field('timetype')->where("uuid=$value[uuid]")->find()['timetype'];
				if ($user_timetype == 0) {
						$user_data[]['uuid'] = $value['uuid'];
				}else{
						$user_time = M('broadcasttime')->where("uuid=$value[uuid]")->select();
						$order_time = date("H",$now_time);
						foreach ($user_time as $key => $value) {
								if ($value['starttime']<=$order_time&&$value['overtime']>=$order_time) {
										$user_data[]['uuid'] = $value['uuid'];
								}
						}
				}
		}
		p($user_data);*/

		// p($level_user);exit();
		// echo $db_member->_sql();exit();
		$systemdata = M("systemsetup")->where("id=4")->where(array("type"=>0))->find();

		$wechaturl = "http://zhubo.heardtech.com/Api/WechatMessage/TemplateMessage";
		foreach ($level_user as $key => $value) {
			$openid = M("wechat")->where("uuid=$value[uuid]")->find()['openid'];
			// $media_name = M("media")->field('media_name')->where("id=$mid")->find()['media_name'];
			// $user_info = M("member")->field('username,nickname')->where("uuid=$value[uuid]")->find();
			// 昵称  自媒体   时限分
			// $this->orderAlidayu($user_info['username'],'SMS_75820075',$user_info['nickname'],$media_name,round($systemdata['value']/60));
			if ($openid) {
				$array[$key]['oid'] = $result;
				$array[$key]['uuid'] = "$value[uuid]";
				$array[$key]['status'] = 0;
				$array[$key]['type'] = 0;
				$array[$key]['time'] = time();
				$array[$key]['overtime'] = $systemdata['value'];

				$warr['uuid'] = urlsafe_b64encode_uuid($value["uuid"]);
				$warr['number'] = urlsafe_b64encode_uuid($number);
				$warr['overtime'] = round($systemdata['value']/60);
				$warr['openid'] = $openid;
				// p($warr);
				$wres = do_curl($wechaturl,$warr);
				// p($wres);
				$arr['arr'][$key]['uid'] = urlsafe_b64encode_uuid($value["uuid"]);
				$arr['arr'][$key]['oid'] = urlsafe_b64encode_uuid($result);
			}
		}
		foreach ($array as $key => $value) {
			M("ordermessage")->add($value);
		}

		// $url = "http://10.1.8.205/Api/Socket/forSocket";
		$url = "http://zhubo.heardtech.com/Api/Socket/forSocket";
		// p($arr);
		if (count($arr['arr'])>0) {
			$res = do_curl($url,$arr);
		}
		// p($res);exit();
		/*给对应等级符合条件的主播群发*/
	}else{
		/*给收藏主播发*/
		$systemdata = M("systemsetup")->where("id=4")->where(array("type"=>0))->find();

		$wechaturl = "http://zhubo.heardtech.com/Api/WechatMessage/TemplateMessage";
		$muuid = M("media")->field('uuid')->where("id=$mid")->find()['uuid'];
		$openid = M("wechat")->where("uuid=$muuid")->find()['openid'];
		$media_name = M("media")->field('media_name')->where("id=$mid")->find()['media_name'];
		$user_info = M("member")->field('username,nickname')->where("uuid=$muuid")->find();
		// 昵称  自媒体   时限分
		$this->orderAlidayu($user_info['username'],'SMS_75820075',$user_info['nickname'],$media_name,round($systemdata['value']/60));
		if ($openid) {
			$array['oid'] = $result;
			$array['uuid'] = "$muuid";
			$array['status'] = 0;
			$array['type'] = 0;
			$array['time'] = time();
			$array['overtime'] = $systemdata['value'];
			M("ordermessage")->add($array);
			$warr['uuid'] = urlsafe_b64encode_uuid($muuid);
			$warr['number'] = urlsafe_b64encode_uuid($number);
			$warr['overtime'] = round($systemdata['value']/60);
			$warr['openid'] = $openid;
			// p($warr);
			$wres = do_curl($wechaturl,$warr);
			// echo json_decode($wres)->code;
			// p(json_decode($wres));
			// $url = "http://10.1.8.205/Api/Socket/forSocket";
			$url = "http://zhubo.heardtech.com/Api/Socket/forSocket";
			$arr['arr'][0]['uid'] = urlsafe_b64encode_uuid($muuid);
			$arr['arr'][0]['oid'] = urlsafe_b64encode_uuid($result);
			// p($arr);
			$res = do_curl($url,$arr);
		}
			// print_r(json_encode(json_decode($res)->data));
		// p(json_decode($res));
			// exit();
		/*给收藏主播发*/
	}
}

}

